﻿
+function ($) {

    $(function () {

        // popover
        $("[data-toggle=popover]").popover();
        $(document).on('click', '.popover-title .close', function (e) {
            var $target = $(e.target), $popover = $target.closest('.popover').prev();
            $popover && $popover.popover('hide');
        });

        // ajax modal
        $(document).on('click', '[data-toggle="ajaxModal"]',
            function (e) {

                e.preventDefault();
                var $this = $(this)
                    , $remote = $this.data('remote') || $this.attr('href')
                    , $modal = $('<div></div>');//用来作容器
                $modal.attr("tips", "用来作容器");
                $('body').append($modal);

                $modal.load($remote, function (response, status, xhr) {
                    var newmodal = $modal.children("div").modal();
                    newmodal.modal('show');

                    //给新加载的界面注册表单验证
                    $.validator.unobtrusive.parse($('#modalcontent form'));
                    $('#modalcontent form').validateBootstrap();
                });
            }
        );

        $(document).on('hidden.bs.modal', function (e) {
            var $container = $(e.target).parents("[tips='用来作容器']");
            if ($container.length === 0)
                $container = $(e.target).parent();

            $container.remove();
        });

        $(document).on('click', '[data-toggle="ajaxLoad"]', function (e) {
            e.preventDefault();
            var $this = $(this), $remote = $this.data('remote') || $this.attr('href');
            window.app.loadUI($remote);
        });


        // dropdown menu
        $.fn.dropdown.Constructor.prototype.change = function (e) {
            e.preventDefault();
            var $item = $(e.target), $checked = false;
            !$item.is('a') && ($item = $item.closest('a'));
            var $menu = $item.closest('.dropdown-menu');
            var $label = $menu.parent().find('.dropdown-label');
            var $labelHolder = $label.text();
            var $select = $item.find('input');
            $checked = $select.is(':checked');
            if ($select.is(':disabled')) return;
            if ($select.attr('type') === 'radio' && $checked)
                return;
            if ($select.attr('type') === 'radio')
                $menu.find('li').removeClass('active');
            $item.parent().removeClass('active');
            !$checked && $item.parent().addClass('active');
            $select.prop("checked", !$select.prop("checked"));

            var $items = $menu.find('li > a > input:checked');
            if ($items.length) {
                var $text = [];
                $items.each(function () {
                    var $str = $(this).parent().text();
                    $str && $text.push($.trim($str));
                });

                $text = $text.length < 5 ? $text.join(', ') : $text.length + ' 被选中';
                $label.val($text);
            } else {
                $label.val($label.data('placeholder') ? $label.data('placeholder') : "");
            }
            $label.valid();
        }
        $(document).on('click.dropdown-menu', '.dropdown-select > li > a', $.fn.dropdown.Constructor.prototype.change);


        // class
        $(document).on('click', '[data-toggle^="class"]', function (e) {
            e && e.preventDefault();
            var $this = $(e.target), $class, $target, $tmp, $classes, $targets;
            !$this.data('toggle') && ($this = $this.closest('[data-toggle^="class"]'));
            $class = $this.data()['toggle'];
            $target = $this.data('target') || $this.attr('href');
            $class && ($tmp = $class.split(':')[1]) && ($classes = $tmp.split(','));
            $target && ($targets = $target.split(','));
            $classes && $classes.length && $.each($targets, function (index, value) {
                if ($classes[index].indexOf('*') !== -1) {
                    var patt = new RegExp('\\s' +
                        $classes[index].
                            replace(/\*/g, '[A-Za-z0-9-_]+').
                            split(' ').
                            join('\\s|\\s') +
                        '\\s', 'g');
                    $($this).each(function (i, it) {
                        var cn = ' ' + it.className + ' ';
                        while (patt.test(cn)) {
                            cn = cn.replace(patt, ' ');
                        }
                        it.className = $.trim(cn);
                    });
                }
                ($targets[index] != '#') && $($targets[index]).toggleClass($classes[index]) || $this.toggleClass($classes[index]);
            });
            $this.toggleClass('active');
        });

        // button loading
        $(document).on('click.button.data-api', '[data-loading-text]', function (e) {
            var $this = $(e.target);
            $this.is('i') && ($this = $this.parent());
            $this.button('loading');
        });

        // table select/deselect all
        $(document).on('change', 'table thead [type="checkbox"]', function (e) {
            e && e.preventDefault();
            var $table = $(e.target).closest('table'), $checked = $(e.target).is(':checked');
            $('tbody [type="checkbox"]', $table).each(function () {
                if (!this.disabled) {
                    this.checked = $checked;
                }
            });
        });

        // dropdown still
        $(document).on('click.bs.dropdown.data-api', '.dropdown .on, .dropup .on, .open .on', function (e) { e.stopPropagation() });

        // tooltip
        $("[data-toggle=tooltip]").tooltip();

        $(document).ajaxStart(function () { Pace.restart(); });
    });
}(jQuery);